package cn.whdream.datastructure.hashtab;

import java.util.HashMap;
import java.util.Scanner;

/**
 * 
 * @author admin
 *  散列表示例代码
 */
public class HashTableDemo {
	private static final int KEY_MAX_SIZE = 10;
	
	public static void main(String[] args) {

		//创建哈希表
				HashTab hashTab = new HashTab(KEY_MAX_SIZE);
				Emp a = new Emp(12, "wangda");
				Emp b = new Emp(11, "daNiu");
				Emp c = new Emp(9, "sss");
				Emp d = new Emp(6, "ccc");
				Emp e = new Emp(26, "eeee");
				hashTab.add(a);
				hashTab.add(b);
				hashTab.add(c);
				hashTab.add(d);
				hashTab.add(e);
				System.out.println("展示搜索结果");
				hashTab.findEmpById(6);
				System.out.println();
				//写一个简单的菜单
				String key = "";
				Scanner scanner = new Scanner(System.in);
				while(true) {
					System.out.println("add:  添加雇员");
					System.out.println("list: 显示雇员");
					System.out.println("find: 查找雇员");
					System.out.println("exit: 退出系统");
					
					key = scanner.next();
					switch (key) {
					case "add":
						System.out.println("输入id");
						int id = scanner.nextInt();
						System.out.println("输入名字");
						String name = scanner.next();
						//创建 雇员
						Emp emp = new Emp(id, name);
						hashTab.add(emp);
						break;
					case "list":
						hashTab.list();
						break;
					case "find":
						System.out.println("请输入要查找的id");
						id = scanner.nextInt();
						hashTab.findEmpById(id);
						break;
					case "exit":
						scanner.close();
						System.exit(0);
					default:
						break;
					}
				}
	}
	private static class Emp {
		public int id;
		public String name;
		public Emp next;
		
		public Emp(int id, String name) {
			super();
			this.id = id;
			this.name = name;
		}

		@Override
		public String toString() {
			return "Emp [id=" + id + ", name=" + name  + "]";
		}
		
	}
	private static class HashTab {
		//为空一个链表
		private EmpLinkedList[] arr;
		private int size;	
		public HashTab(int size) {
			super();
			this.size = size;
			this.arr = new EmpLinkedList[size];
			for (int i = 0; i < arr.length; i++) {
				this.arr[i] = new EmpLinkedList();
			}
		}
		public void findEmpById(int id) {
			int index = haveFun(id);
			EmpLinkedList list = arr[index];
			list.find(id);
		}
		public void add(Emp emp) {
			if (emp == null || emp.id < 0) {
				return;
			}
			int index = haveFun(emp.id);
			EmpLinkedList current = arr[index];
			current.add(emp);
			current.list();
		}
		private int haveFun(int id) {
			return id%size;
		}
		
		public void list() {
			for (int i = 0; i < arr.length; i++) {
				System.out.print("数组"+(i+1));
				arr[i].list();
				System.out.println();
			}
		}
		
	}
	private static class EmpLinkedList {
		public Emp head;
		public void add(Emp emp) {
			if(head == null) {
				head = emp;
				return;
			}
			Emp cur = head;
			while(true) {
				if (cur.next == null) {
					break;
				}
				 cur = cur.next;
			}
			cur.next = emp;
			System.out.println("EmpLinkedList add： over");
		}
		
		public void find(int id) {
			Emp cur = head;
			if (cur == null) {
				System.out.println("链表为空");
				return;
			}
			while(cur != null) {
				if(cur.id == id) {
					System.out.print(cur+" ");
					break;
				}
				if (cur.next == null) {
					System.out.println("没有找到");
					break;
				}
				 cur = cur.next;
			}
		}

		public void list(){
			Emp cur = head;
			if (cur == null) {
				System.out.println("链表为空");
				return;
			}
			while(true) {
				System.out.print(cur+": ");
				if (cur.next == null) {
					break;
				}
				 cur = cur.next;
			}
		}
	}
}
